<?php

class Albums extends Admin_Controller {

    public function __construct() {
        parent::__construct();
        
        //Load albums model
        $this->load->model('albums_m');
    }

    /**
     * Default function
     */
    public function index() {
        //Get arrAlbums
        $arrAlbum = $this->albums_m->get_all_album();
        
        //Assign to view
        $this->assign('arrAlbum', $arrAlbum);
    }

    /**
     * Create new album page
     */
    public function create() {
        //Load category model
        $this->load->model('category_m');

        //Get arrCategory
        $arrCategory = $this->category_m->get_tree();

        //Assign to view
        $this->assign('arrCategory', $arrCategory);

        //Append CSS stylesheet
        $this->addStylesheet(base_url() . 'static/css/jquery.fileupload-ui.css');
    }
    
    /**
     * Create new album page
     */
    public function createNew() {
        //Load category model
        $this->load->model('category_m');

        //Get arrCategory
        $arrCategory = $this->category_m->get_tree();

        //Assign to view
        $this->assign('arrCategory', $arrCategory);

        //Append CSS stylesheet
        $this->addStylesheet(base_url() . 'static/js/swfupload/default.css');
    }
    
    /**
     * Create album ajax
     */
    public function create_ajax() {
        //Set response default
        $response = array(
            'error' => 1,
            'message' => 'Error Message'
        );

        if ($this->input->post()) {

            $params = $this->input->post();

            //Set Album params
            $albumData = array(
                'category_id' => $params['cateID'],
                'name' => $params['albumName'],
                'slug' => $this->removeUnicode($params['albumName']),
                'info' => $params['albumInfo'],
                'thumb' => $params['photoThumb'][0],
                'status' => isset($params['albumStatus']) ? 0 : 1,
                'total_photo' => count($params['photoName']),
                'create_date' => time()
            );

            //Insert album
            $albumID = $this->albums_m->insert_album($albumData);

            if ($albumID) {
                foreach ($params['photoName'] as $key => $name) {
                    $photoData = array(
                        'album_id' => $albumID,
                        'name' => $name,
                        'url' => $params['photoUrl'][$key],
                        'thumb_url' => $params['photoThumb'][$key],
                        'create_date' => time()
                    );
                    
                    $this->albums_m->insert_photo($photoData);
                }
                
                $response['error'] = 0;
                $response['message'] = 'Album đã được tạo.';
                $response['id'] = $albumID;
            }
            else {
                $response['message'] = 'Có lỗi xảy ra.';
            }
        }

        //Return response
        echo json_encode($response);
        exit;
    }
    
    /**
     * delete category ajax
     */
    public function delete_ajax() {
        //Set response default
        $response = array(
            'error' => 1,
            'message' => 'Error Message'
        );

        if ($this->input->post()) {

            $album_id = $this->input->post('albumID');

            //Check empty album
            if (empty($album_id)) {
                $response['message'] = 'Không tìm thấy album này.';
                //Return response
                echo json_encode($response);
                exit;
            }

            //Delete categories
            $result = $this->albums_m->delete_album($album_id);

            //Check result
            if ($result) {
                $response['error'] = 0;
                $response['message'] = 'Album đã được xóa.';
            } else {
                $response['message'] = 'Có lỗi xảy ra, vui lòng thử lại.';
            }
        }

        //Return response
        echo json_encode($response);
        exit;
    }
    
    /**
     * Edit album by $id
     */
    public function edit()
    {
        //Load category model
        $this->load->model('category_m');
        
        //Get params
        $id = $this->uri->segment(5);
        
        if (empty($id)) {
            redirect('backend/albums');
        }
        
        //Get album detail
        $arrAlbum = $this->albums_m->get_album_detail($id);
        
        //Get arrCategory
        $arrCategory = $this->category_m->get_tree();
        
        //Assign to view
        $this->assign('arrCategory', $arrCategory);
        $this->assign('arrAlbum', $arrAlbum);

        //Append CSS stylesheet
        $this->addStylesheet(base_url() . 'static/css/jquery.fileupload-ui.css');
    }

}